package situ.autoweter.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.javafx.collections.MappingChange;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import situ.autoweter.common.NumberToCN;
import situ.autoweter.dao.UsUserDao;
import situ.autoweter.dao.paywindowDao;
import situ.autoweter.entity.*;
import situ.autoweter.service.*;

import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

@Controller
public class PayWindowController {
    @Autowired
    UsUserDao usUserDao;
    @Autowired
    PyUserhistoryService pyUserhistoryService;//用户历史详单
    @Autowired
    PyPayService pyPayService;//交费单
    @Autowired
     IvInvoiceService invoiceService;//发票表
    @Autowired
    PyBillService pyBillService;//水费表
    @Autowired
    PyBilldetailService pyBilldetailService;
    @Autowired
    PyBalanceService pyBalanceService;//销账明细表
    @Autowired
    paywindowDao dao;

    @RequestMapping("/pay_window.html")
    public String paywindow(Map map, @RequestParam(required = false, value = "UserNo") String no,
                            @RequestParam(required = false,defaultValue = "BillNo")String ID) {
        UsUser user = usUserDao.selectUsUserById(no);
        if (user != null) {

            if (user.getUserMoney() > 0) {
                map.put("money", user.getUserMoney());
                map.put("money01", 0.00);
            } else {
                map.put("money01", user.getUserMoney());
                map.put("money", 0.00);
            }
        } else {
            user = new UsUser();
        }
        //欠费单
        List<PyBill> pyBills = pyBillService.queryByUserNo(no);
        //水费单详单
        //用户交费单
        List<Map> pyPays = dao.queryPayByUserNo(no);
        //历史详单
        List<Map> UserHistorys = dao.queryPY_UserHistoryByUserNo(no);
        //催缴水费
        double count=0.00;
        for (PyBill pb:pyBills){
            count+=pb.getBillMoney()-pb.getRealMoney();
        }
        List<Map> chui = dao.queryChui(no);
        Date d=new Date();
        SimpleDateFormat f=new SimpleDateFormat("YYYY年MM月dd日");
        String date =f.format(d);

        map.put("date", date);
        map.put("chui",chui);
        map.put("count", count);
        map.put("UserHistorys", UserHistorys);
        map.put("pyPays", pyPays);
        map.put("pyBills",pyBills);
        map.put("user", user);




        return "page/pay_window.html";
    }

    @RequestMapping("/sys_selectUser01.html")
    public String selectUser(Map map, UsUser user,
                             @RequestParam(defaultValue = "1", required = false) int startPage,
                             @RequestParam(defaultValue = "5", required = false) int pagesize) {
        PageHelper.startPage(startPage, pagesize);
        List<UsUser> usUsers = usUserDao.selectByMony(user);
        PageInfo pageInfo = new PageInfo<UsUser>(usUsers, 5);
        map.put("pageInfo", pageInfo);
        map.put("startPage", startPage);
        map.put("pagesize", pagesize);
        map.put("users", usUsers);

        return "page/sys_selectUser01.html";
    }


    @RequestMapping("/pay_window_selectInvoice01.html")
    public String selectInvoiceNo(Map map,
                                  @RequestParam(defaultValue = "1", required = false) int startPage,
                                  @RequestParam(defaultValue = "100", required = false) int pagesize) {
        PageHelper.startPage(startPage, pagesize);
        List<Map> list = dao.selectInvoiceNo();
        PageInfo pageInfo = new PageInfo<Map>(list, 5);
        map.put("pageInfo", pageInfo);
        map.put("startPage", startPage);
        map.put("pagesize", pagesize);
        map.put("list", list);
        return "page/pay_window_selectInvoice01.html";
    }

    /*
        1.添加用户历史详单--PyUserhistory中有
        2.添加交费单--PyPay
        3.修改发票使用状态--IvIncoice
        4.查询水费单PyBill 一对多 查询水费详情单PyBill(未配置)
        5.对水费单进行销账处理
        6.销账明细和销账明细的明细 PyBalance 和 PyBalance
        */
    @RequestMapping("/pay_printInvoice.html")//在这里面做销账和添加缴费单和用户历史详单
    public String pay_printInvoice(Map map, UsUser user, @RequestParam(value = "M", required = false) double M,
                                   @RequestParam(value = "fapiao", required = false) String fapiao) {
        user = usUserDao.selectUsUserById(user.getUserNo());
        user.setUserMoney(user.getUserMoney()+M);

        //本次交费金额
        map.put("M", M);
        String Mto = NumberToCN.number2CNMontrayUnit(M);
        map.put("Mto", Mto);
        //发票号码已使用
        map.put("fapiao", fapiao);
        IvInvoice ivInvoice = new IvInvoice();
        ivInvoice.setInvoiceNo(fapiao);
        ivInvoice.setUsed(1);
        invoiceService.updateNotNullById(ivInvoice);
        //年月份
        Date d = new Date();
        SimpleDateFormat format = new SimpleDateFormat("YYYY年MM月dd日");
        SimpleDateFormat format1 = new SimpleDateFormat("YYMM");
        String paNoyear = format1.format(d);
        String day = format.format(d);//打印日期
        map.put("day", day);
        //交费单号
        String payNo = "JF" + user.getUserNo() + "-" + paNoyear + "-" + d.getHours();
        map.put("payNo", payNo);
        //交费时间
        d.toInstant();
        //以上没有对数据库进行操作
        //添加历史详单
        PyUserhistory pyUserhistory = new PyUserhistory();
        pyUserhistory.setUserNo(user.getUserNo());
        pyUserhistory.setDatee(d.toInstant());
        pyUserhistory.setOrderNo(payNo);
        pyUserhistory.setPayMoney(M);
        pyUserhistory.setUserMoney(user.getUserMoney());
        if(user.getUserMoney()<0){
            pyUserhistory.setBillMoney(-user.getUserMoney());
        }
        pyUserhistoryService.saveNotNull(pyUserhistory);

        usUserDao.updateNotNullUsUserById(user);
        map.put("user", user);
        //查询水费单，开始销账处理，添加销账明细和销账明细的明细

        List<PyBill> pyBills = pyBillService.queryByUserNo(user.getUserNo());
        double M2 = M;
        for (PyBill pyBill : pyBills) {//循环进行销账处理
                for (PyBilldetail pyBilldetail : pyBill.getPyBilldetails()) {
                        if (M2 > 0) {
                            if (M2 >= pyBilldetail.getDetailMoney()) {
                                pyBilldetail.setRealMoney(pyBilldetail.getDetailMoney());//已结清
                                pyBilldetail.setBalance(1);
                                pyBilldetail.setBalanceDate(d.toInstant());
                                M2=M2-pyBilldetail.getDetailMoney();

                                pyBill.setRealMoney(pyBill.getRealMoney()+pyBilldetail.getDetailMoney());//水费单实收
                            } else {
                                pyBilldetail.setRealMoney(0.00);
                                pyBilldetail.setDetailMoney(pyBilldetail.getDetailMoney()-M2);//未结清

                                pyBill.setBillMoney(pyBill.getBillMoney()-M2);
                                pyBill.setRealMoney(pyBill.getRealMoney()+M2);//水费单实收
                                M2=0;
                            }
                            pyBilldetailService.updateNotNullById(pyBilldetail);
                            if (pyBill.getBillMoney()-pyBill.getRealMoney()==0){
                                pyBill.setBalance(1);
                                pyBill.setBalanceDate(d.toInstant());
                            }
                            pyBillService.updateNotNullById(pyBill);
                        }else{
                            break;
                        }
                }
                if(M2<=0){
                    break;
                }
        }
        //至此销账处理完成
        //交费单
        PyPay pyPay = new PyPay();
        pyPay.setPayNo(payNo);
        pyPay.setUserNo(user.getUserNo());
        pyPay.setEmpID(1);
        pyPay.setPayType("坐收");
        pyPay.setPayDate(d.toInstant());
        pyPay.setPayMoney(M);
        pyPay.setInvoice(fapiao);
        pyPay.setUseMoney(M-M2);
        pyPayService.saveNotNull(pyPay);
        return "page/pay_printInvoice.html";
    }

    //修改用户信息
    @RequestMapping("/updateUsUser")
    public String updateUsUser(UsUser user){
        usUserDao.updateNotNullUsUserById(user);
        return "redirect:/pay_window.html?UserNo="+user.getUserNo();
    }
    @Autowired
    SyEmpService syEmpService;
    //发票补开
    @RequestMapping("/pay_window_reInvoice01.html")
    public String paywindowreInvoice(Map map,PyPay pyPay,IvInvoice ivInvoice){
            pyPay=pyPayService.findOne(pyPay.getPayNo());
            ivInvoice=invoiceService.findOne(pyPay.getInvoice());
            System.out.println(ivInvoice);
            map.put("pyPay", pyPay);
            map.put("ivInvoice", ivInvoice);
            map.put("user",usUserDao.selectUsUserById(pyPay.getUserNo()));
            map.put("emp", syEmpService.findOne(pyPay.getEmpID()).getEmpNmae());


        return "page/pay_window_reInvoice01.html";
    }



}

